# -*- coding: utf-8 -*-
import logging
import re

_LOGGER = logging.getLogger(__name__)
# TODO: set this in a config file
clean_re = "[ _\,\.\(\)\[\]\-](\
ac3.*|avc|\
bigf|bluray.*|blueray|bd5|brrip|bdrip|\
cam|custom|\
dts|dc|dl|divx|divx5|dsr|dsrip|dutch|dvdrip|dvdscr|dvdscreener|dvdivx|dvd|dd2\ 0|dxva|\
fragment|frenh|fs|forcebleue|\
german|\
hdtv|hdrip|hdtvrip|hrhd|hrhdtv|hddvd|hd|\
internal|\
klinehd|\
legion|lechti|limited|\
multilang|multisubs|multi|\
nerdhd|nfofix|ntsc|\
ogg|ogm\
|pal|patologik|pdtv|proper\
read\.nfo|repack|remux|rerip|retail|rld|r3|r5|\
screener|seth|slimhd|stv|svcd|swedish|ssl|\
tftd|trsiel|\
unrated|\
vostfr|\
wazatt|ws|\
telesync|ts|telecine|tc|\
4khd|480p|480i|576p|576i|720p.*|720i|1080p.*|1080p\-.*1080i|\
x264.*|h264|h\ 264|h\ 264\ .*|264|web|web\-dl|xvid|xvidvd|xxx|www.*|cd[1-9]|\[.*\])"

table={
	"mgl-garf2720p" : "garfield 2",
	"ulshd-tintin.1080p" : "tintin",
	"nrdhd-salsepareille720" : "les schtroumpfs",
	"nrdhd-happyfeet21080" : "happy feet two",
	"nrdhd-connargion1080" : "contagion",
	"s8-bdmds-1080p" : "super 8",
	"lost.territoires-720p" : "territories",
	"lost-thing.1080p" : "the thing",
	"dupli-the.devils.rock.1080p" : "the devils rock",
	"s8-transit-1080p" : "transit",
	"L'Orphelinat (2007) 1080P BluRay Multi DTS-HD MA 5.1-Cortex" : "orphelinat",
	"l'orphelinat (2007) ma 5 1 cortex" : "orphelinat",
	"02 - FEE_CLOCHETTE_LA PIERRE DE LUNE_FR" : "la fee clochette et la pierre de lune",
#	"LA FEE CLOCHETTE L'EXPEDITION FEERIQUE.LiMiTED.French.BRRip.XviD.AC3-TFTD" : "Tinker Bell and the Great Fairy Rescue",
	"Joyeux Noel Madagascar_dVDrIP_Xvid_Nabil@udon" : "joyeux noel madagascar",
	"lost-pib.1080p" : "Puss in boots",
	"Slash.Live.Made.In.Stoke.2011.720p.MBluRay.PROPER.x264-SEMTEX" : "Slash: Made in Stoke",
	"friends-drive.2011.multi.vff.dts-hdma-1080p" : "drive",
	"nrdhd-chmipfuck720" : "Alvin and the Chipmunks ChipWrecked",
	"LE.MARIAGE.DE.RAIPONCE.FRENCH.1080P-TDLP"  : "Le mariage de raiponce",
	"lost-shame.720p" : "shame",
	"Joyeux Noel Shrek DVD-R NTSC" : "Joyeux Noel Shrek",
	"ulshd-hysteria.1080p" : "hysteria",
	"La.Fée.Clochette.1.Tinker.Bell.2008.BluRay.720p.FRENCH.AC3" : "la fée clochette 1",
	"hunger 2009" : "hunger",
	"Hunger.2009.MULTi.1080p.BluRay.x264-LEGiON" : "affamés",
	"nos voisins les hommes overthehedge.h264.ac3.5.1.fr.en.stfr" : "nos voisins les hommes",
	"Chronicle.2012.Director's.Cut.FRENCH.BluRay.1080p.x264-Chronicle" : "chronicle",
	"Aladdin.1992.720p.HDTV.x264-hV_Fr" : "Aladdin",
	"le roi lion" : "The lion king",
	"friends-le.roi.lion.2.1998.truefrench-720p" : "le roi lion 2 : simba pride",
	"friends-le.roi.lion.3.2004.truefrench-720p" : "The Lion King 3",
	"s8-livide.1080p" : "livide",
	"Monstres contre Alliens.1080P.Multi.AC3.BluRay.X264-RLD" : "Monstres contre Aliens",
	"Lilo & Stitch 2" : "lilo et stitch 2 stitch has a glitch",
	"Le.Territoire.des.Loups.2012.FRENCH.VFF.1080p.BluRay.Remux.AVC-Sookie" : "le territoire des loups",
	"deal-aserbianfilm.multi.1080p":"a serbian film",
	"shrek forever after 2010" : "shrek forever after",
	"the incident" : "asylum blackout",
	"la haut 2009" : "up",
	"the girl next door 2007" : "Jack Ketchum's Evil",
	"The.Bomber.2011.Part1.FRENCH.1080i.Bluray.Remux.AVC-Sookie" : "the bomber",
	"The.Bomber.2011.Part2.FRENCH.1080i.Bluray.Remux.AVC-Sookie" : "the bomber",
	"ulshd-101d 720p" : "101 dalmatians",
	"So.You.Want.To.Be.a.Pirate.2012.TRUEFRENCH.DVDRip.XviD-UTT" : "So You Want To Be a Pirate",
	"Tinker.Bell.Secret.of.the.Wings.2012.FRENCH.BDRiP.XviD-ARTEFAC" : "Tinker Bell Secret of the Wings",
	"airline-alyce_1080p" : "alyce",
	"friends-dark.shadows.2012.milti.truefrench.dts-hdma.ac3-1080p" : "dark shadows",
	"lost-grandsoir.1080p" : "le grand soir",
	"lost-radiostars.720p" : "radiostars",
	"mgl-tcitw1080p" : "The.Cabin.In.The.Woods",
	"lost-mad3.720p" : "Madagascar.3.Europes.Most.Wanted",
	"lost-promt-1080" : "Prometheus",
	"lost-swath.1080p" : "Snow.White.and.the.Huntsman",
	"ulshd-seeking-1080p" : "Seeking.a.Friend.for.the.End.of.the.World",
}

class RegexpBasedScanner(object):
	def __init__(self, *scanners):
		self.scanners = scanners

	def add_scanner(self, regexp, scanner, cleaner, media_type=""):
		self.scanners.append((regexp, scanner, cleaner, media_type))

	def clean_title(self, title):
		for regexp, _, cleaner, _ in self.scanners:
			match = re.match(regexp, title)
			if match:
				return cleaner.clean_title(title)

	def search(self, title, path=None):
		for regexp, scanner, cleaner, media_type in self.scanners:
			regexp = re.compile(regexp, re.I)
			match = regexp.match(title)
			if match:
				_LOGGER.info("%s match with scanner %s of type %s", title, scanner, media_type)
				params = match.groupdict()
				show_name = params.pop("show_name")
				return scanner.search(cleaner.clean_title(table.get(show_name, show_name)), **params)

class TitleCleaner(object):
	def __init__(self, pattern_cleaners):
		self.__pattern_cleaners = pattern_cleaners

	def clean_title(self, title):
		title = title.lower()
		for pattern, replace in self.__pattern_cleaners:
			_LOGGER.debug("clean title %s with pattern %s, and replace with |%s|", title, pattern, replace)
			title = re.sub(pattern, replace, title)
		_LOGGER.info("Got clean title: %s", title)
		return title.strip()

class TVShowCleaner(TitleCleaner):
	clean_strings_pattern = [
		("([\.-])", " "),
		("[s]\d\d[e]\d\d([\w\s]+)", ""),
		("([_])", " "),
		(clean_re + "([_\,\.\(\)\[\]\-]|$)", ""),
		(clean_re, ""),
		("(slimhd|frenh|french|truefrench|www.*|xvid.*|subforced.*)", ""),
		("(\[.*\])", ""),
		("( 19[0-9][0-9]|20[0-1][0-9] [\w\s]+)", ""),
	]
	def __init__(self):
		TitleCleaner.__init__(self, self.clean_strings_pattern)

class MovieCleaner(TitleCleaner):
	clean_strings_pattern = [
		("([\.-])", " "),
		("([_])", " "),
		("\s(\d\d\d\d.*)", ""),
		("([\(\)])", " "),
		(clean_re + "([_\,\.\(\)\[\]\-]|$)", ""),
		(clean_re, ""),
		("(french|truefrench|www.*|xvid.*|subforced.*)", ""),
		("(\[.*\])", ""),
		("[. ]+(19[0-9][0-9]|20[0-1][0-9])", ""),
	]
	def __init__(self):
		TitleCleaner.__init__(self, self.clean_strings_pattern)

